import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { appox } from '@/appox/appox'

export const useLogStore = defineStore('log', () => {
  // 状态
  const currentLogSource = ref(null)
  const logs = ref(new Map())
  const logSources = ref([])
  const autoScroll = ref(true)  // 新增自动滚动状态

  // 添加日志
  function addLog(message, source) {
    if (!logs.value.has(source)) {
      // 新日志源
      logs.value.set(source, [message])
      logSources.value.push(source)
      
      // 如果是第一个日志源，自动选中
      if (logSources.value.length === 1) {
        currentLogSource.value = source
      }
    } else {
      // 已有日志源，追加消息
      logs.value.get(source).push(message)
    }
  }

  // 切换日志源
  function switchLogSource(source) {
    currentLogSource.value = source
  }

  // 清空当前日志
  function clearCurrentLog() {
    if (currentLogSource.value) {
      logs.value.set(currentLogSource.value, [])
    }
  }

  // 获取当前日志内容
  const currentLogs = computed(() => {
    if (!currentLogSource.value) return []
    return logs.value.get(currentLogSource.value) || []
  })

  // 切换自动滚动状态
  function toggleAutoScroll() {
    autoScroll.value = !autoScroll.value
  }

  // 开发调试用
  function actionForDevel() {
    appox.print("Testing");
  }

  return {
    currentLogSource,
    logSources,
    currentLogs,
    autoScroll,
    addLog,
    switchLogSource,
    clearCurrentLog,
    toggleAutoScroll,
    actionForDevel
  }
}) 